#include "qqutils.h"

QList<QQMessage> QQUtils::parseMessage(const QString& content) {
    QList<QQMessage> messages;
    QQMessage currentMsg;
    bool hasHeader = false;

    // 强化正则：支持中文、英文、点号，排除命令符号
    static QRegularExpression headerRe(
        R"(^([\s\S]+): (\d{2}-\d{2} \d{2}:\d{2}:\d{2})$)",
        QRegularExpression::UseUnicodePropertiesOption
        );
    const auto lines = content.split('\n', Qt::SkipEmptyParts);
    for (const auto& rawLine : lines) {
        QString line = rawLine.trimmed();
        QRegularExpressionMatch match = headerRe.match(line);

        if (match.hasMatch()) {
            if (hasHeader) {
                messages.append(currentMsg);
                currentMsg = QQMessage();
            }
            currentMsg.user = match.captured(1);
            currentMsg.timestamp = match.captured(2);
            hasHeader = true;
        } else {
            if (hasHeader) {
                // 保留原始行内容（包括前导/后置空格）
                currentMsg.content.append(rawLine);
            }
        }
    }

    if (hasHeader && !currentMsg.content.isEmpty()) {
        messages.append(currentMsg);
    }

    return messages;
}
